package com.zz.study.sharding.util;

import com.alibaba.fastjson.JSON;
import com.zz.study.sharding.entity.GPSLocation;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * created by xjx on  2023-04-06 14:14
 */
public class GPSUtil {


    /**
     * 将经纬度转换为度分秒格式
     * @param du 116.418847
     * @return 116°25'7.85"
     */
    public static String changeToDFM(double du) {
        int du1 = (int) du;
        double tp = (du - du1) * 60;
        int fen = (int) tp;
        String miao = String.format("%.2f", Math.abs(((tp - fen) * 60)));
        return du1 + "°" + Math.abs(fen) + "'" + miao + "";
    }

    /**
     * 度分秒转经纬度
     * @param dms 116°25'7.85"
     * @return 116.418847
     */
    public static double changeToDu(String dms) {
        if (dms == null) return 0;
        try {
            dms = dms.replace(" ", "");
            String[] str2 = dms.split("°");
            if (str2.length < 2) return 0;
            int d = Integer.parseInt(str2[0]);
            String[] str3 = str2[1].split("\'");
            if (str3.length < 2) return 0;
            int f = Integer.parseInt(str3[0]);
            String str4 = str3[1].substring(0, str3[1].length() - 1);
            double m = Double.parseDouble(str4);

            double fen = f + (m / 60);
            double du = (fen / 60) + Math.abs(d);
            if (d < 0) du = -du;
            return du;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }





    public static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    public static double getDistance(double lng1,double lat1, double lng2, double lat2
                                      ) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        return s * 1000;
    }

    /**
     * 判断一个点是否在圆形区域内，单位米
     */
    public static boolean isInCircle(String checkPoint,String circlePoint, String circleRadius) {
        double lng = JSON.parseObject(checkPoint).getDouble("longitude");
        double lat = JSON.parseObject(checkPoint).getDouble("latitude");
        checkPoint = lng + "," + lat;
        String[] split = checkPoint.split(",");
        String[] split1 = circlePoint.split(",");
        return getDistance(Double.valueOf(split[1].trim()), Double.valueOf(split[0].trim()), Double.valueOf(split1[1].trim()), Double.valueOf(split1[0].trim())) < Double.parseDouble(circleRadius);
    }

    public static boolean isInPolygon(String checkPoint, List<String> points) {
        // 将要判断的横纵坐标组成一个点
        double lng = JSON.parseObject(checkPoint).getDouble("longitude");
        double lat = JSON.parseObject(checkPoint).getDouble("latitude");
        checkPoint = lng + "," + lat;
        String[] split1 = checkPoint.split(",");
        Point2D.Double point1 = new Point2D.Double(Double.valueOf(split1[0].trim()), Double.valueOf(split1[1].trim()));
        // 将区域各顶点的横纵坐标放到一个点集合里面
        List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
        double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
        for (String point : points) {
            String[] split = point.split(",");
            polygonPoint_x = Double.valueOf(split[0].trim());
            polygonPoint_y = Double.valueOf(split[1].trim());
            Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
            pointList.add(polygonPoint);
        }
        return check(point1, pointList);
    }

    public static boolean isInPolygon(GPSLocation checkPoint, List<GPSLocation> points) {
        // 将要判断的横纵坐标组成一个点
        double lng = checkPoint.getLongitude();
        double lat = checkPoint.getLatitude();
        Point2D.Double point1 = new Point2D.Double(lng, lat);
        // 将区域各顶点的横纵坐标放到一个点集合里面
        List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
        double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
        for (GPSLocation point : points) {
            polygonPoint_x = point.getLongitude();
            polygonPoint_y = point.getLatitude();
            Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
            pointList.add(polygonPoint);
        }
        return check(point1, pointList);
    }

    /**
     * 一个点是否在多边形内
     *
     * @param point
     *            要判断的点的横纵坐标
     * @param polygon
     *            组成的顶点坐标集合
     * @return
     */
    private static boolean check(Point2D.Double point, List<Point2D.Double> polygon) {
        java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();

        Point2D.Double first = polygon.get(0);
        // 通过移动到指定坐标（以双精度指定），将一个点添加到路径中
        peneralPath.moveTo(first.x, first.y);
        polygon.remove(0);
        for (Point2D.Double d : polygon) {
            // 通过绘制一条从当前坐标到新指定坐标（以双精度指定）的直线，将一个点添加到路径中。
            peneralPath.lineTo(d.x, d.y);
        }
        // 将几何多边形封闭
        peneralPath.lineTo(first.x, first.y);
        peneralPath.closePath();
        // 测试指定的 Point2D 是否在 Shape 的边界内。
        return peneralPath.contains(point);

    }



}
